<?php
/**
 * @version 0.8.3 2010-10-07
 * @package Joomla
 * @subpackage LeagueResults
 * @copyright (C)  2008, 2009, 2010 Henryk Drozd
 * @license GNU/GPL, see LICENSE.php
 * LeagueResults is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License 2
 * as published by the Free Software Foundation.

 * LeagueResults is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die('Restricted access');

jimport('joomla.application.component.model');

// League Season Table
class lrModelLstable extends JModel
{
	var $_id = null;
	var $_seasonid = null;
	var $_lstable = null;
	
	
	function __construct()
	{
	parent::__construct();
	$_id = JRequest::getVar ('id', 0);
	$_seasonid = JRequest::getVar ('seasonid', 0);
	$this->_id = $_id;
	$this->_seasonid = $_seasonid;
	}
	
	
	function getList()
	{
	if (JRequest::getVar ('latest') == 'yes')
		$this->_id = $this->getTableid($this->_seasonid);
	
	if (!$this->_lstable)
		{
		$query = "SELECT  tc.tableheaders_id, th.leagueseasons_id, s.longname, s.year,
						s.noofteams, s.promoted, s.semipromoted,
						s.relegated, s.semirelegated, th.tname, th.tdate, tc.position,
						tc.team_id, t.tablename, t.tbadge, tc.matches, tc.points, tc.wins,
						tc.draws, tc.defeats, tc.scoreplus, tc.scoreminus, tc.published, tc.id, th.pointsformat,
						c.url, t.club_id
					FROM #__lr_tablecontents AS tc
					JOIN #__lr_tableheaders AS th ON th.id = tc.tableheaders_id
					JOIN #__lr_teams AS t ON tc.team_id = t.id				
					JOIN #__lr_clubs AS c ON t.club_id = c.id				
					JOIN #__lr_leagueseasons AS s ON th.leagueseasons_id = s.id
					WHERE th.id = '".$this->_id."'
					ORDER BY position";
				
				
				
		$this->_lstable = $this->_getList($query, 0, 0);
		}

	return $this->_lstable;
	}

    function getTableid($seasonid)
    {
        // get a reference to the database
        $db = &JFactory::getDBO();
 
        
		$query = "SELECT * FROM `#__lr_tableheaders` WHERE leagueseasons_id = '"
					.$seasonid."' ORDER BY tdate DESC LIMIT 1";

		$db->setQuery($query);
		$tableheader = $db->loadObject();
        return $tableheader->id;
      
    }
	
}
?>